#ifndef __GLWIDGET_H__
#define __GLWIDGET_H__

#include <QGLWidget>
#include <QMouseEvent>
#include <QFile>
#include <QThread>

#include "src/solver.h"
#include "src/input.h"
#include "defines/global.h"

class GLWidget : public QGLWidget {
	Q_OBJECT

    public:
        GLWidget(QWidget* = 0);
        ~GLWidget();

        QSize minimumSizeHint() const;
        QSize sizeHint() const;
		std::vector<Fabric *> loadFile(QFile*, unsigned int, double, double, unsigned int, bool, double, double, bool, int, double, double, double, Vector3D);
		std::vector<Fabric *> loadFile(QFile*);

		Solver *changeSolver(unsigned int, double, double, bool, double, double, bool, int, double, double, double, Vector3D);

		const std::vector<Fabric *> &getFabrics(){return fabrics;};
		std::vector<Fabric *> *getPlaybackFabrics(){return playbackFabrics;};

        void stopSimulation();

        Solver *getSolver() {return solver;};

        Input *getInput() {return &input;};

		void addObject(Object *_o){objects.push_back(_o);};
		void deleteObject(Object *);

		void deleteFabric(Fabric *);

    public slots:
        bool startPauseSimulation(bool);

        void changeWireframe(bool);
        void changeShowAxis(bool);
        void changeShowGrid(bool);
        void changeShowFloor(bool);
        void changeShowMast(bool);
        void changeShowNormal(bool);
        void changeDrawSmooth(bool);
        void changeDrawPlayback(bool);

        void setXRotation(int angle);
        void setYRotation(int angle);
        void setZRotation(int angle);

        void restartSimulation();
        void recordSimulation(QString);
        void recordSimulation(bool, QFile * = NULL);

    signals:
        void xRotationChanged(int angle);
        void yRotationChanged(int angle);
        void zRotationChanged(int angle);

    protected:
        void initializeGL(void);
        void paintGL(void);
        void resizeGL(int,int);
        void wheelEvent(QWheelEvent *);
        void mousePressEvent(QMouseEvent *);
        void mouseMoveEvent(QMouseEvent *);

	public slots:
        void calculate();
        void enterCS();
        void leaveCS();

    private:
        void normalizeAngle(int *angle);





        //***cada tecido é pra ter sua textura!!!
        GLuint fabricTexture;
        QImage texture;




        double zoom;
        double xMov;
        double yMov;
        double zMov;
        int xRot;
        int yRot;
        int zRot;
        QPoint lastPos;
        bool wireframe;
        bool showAxis;
        bool showGrid;
        bool showFloor;
        bool showMast;
        bool showNormal;
        bool drawSmooth;
        bool drawPlayback;
        bool record;

//        Sphere *sphere;
//        Table *table;
        std::vector<Fabric *> fabrics;
        std::vector<Object *> objects;

        std::vector<Fabric *> *playbackFabrics;
//        std::vector<Object *> playbackObjects;
        Solver *solver;

        Input input;

        GLfloat light0_pos[4];
        GLfloat light0_ambient[4];
        GLfloat light0_diffuse[4];
        GLfloat light0_specular[4];
        GLfloat global_ambient[4];

		QTimer *timer;
		QThread *thread;
		QMutex fabricMutex;
//		double t;
        QFile *playback;
};

#endif
